package com.ttms.service.impl;

import com.ttms.dao.SeatDao;
import com.ttms.dao.StudioDao;
import com.ttms.dao.TicketDao;
import com.ttms.dao.impl.SeatDaoImpl;
import com.ttms.dao.impl.StudioDaoImpl;
import com.ttms.dao.impl.TicketDaoImpl;
import com.ttms.pojo.Seat;
import com.ttms.pojo.Studio;
import com.ttms.pojo.Ticket;
import com.ttms.service.SeatService;

import java.util.ArrayList;
import java.util.List;

public class SeatServiceImpl implements SeatService {
    SeatDao seatDao = new SeatDaoImpl();
    TicketDao ticketDao = new TicketDaoImpl();
    StudioDao studioDao= new StudioDaoImpl();

    @Override
    public List<StringBuffer> getSeatMap( Integer sched_id,Integer studio_id) {
        Studio studio = studioDao.queryStudioById(studio_id);
        List<Seat> seats = seatDao.findSeatByStudioId(studio_id);
        ArrayList<StringBuffer> map = new ArrayList<StringBuffer>();
        for (int i = 1; i <= studio.getStudio_row_count(); i++) {
            StringBuffer stringBuffer = new StringBuffer();
            for (int j = 1; j <= studio.getStudio_col_count(); j++) {
                Seat seat = seatDao.findSeatByRowAndCol(studio_id, i, j);
                Ticket ticket = ticketDao.findTicketIdBySeatId(sched_id, seat.getSeat_id());

                    if (seat.getSeat_status() == 1) {
                        if (ticket==null){
                            stringBuffer.append("a");
                        }else {
                            stringBuffer.append("_");
                        }
                    }
                    if (seat.getSeat_status() == -1) {
                        stringBuffer.append("_");
                    }

            }
            map.add(stringBuffer);

        }
        System.out.println(map);
        return map;
    }

    @Override
    public Seat getSeatId(int studio_id,int row,int col) {
        return seatDao.findSeatByRowAndCol(studio_id,row,col);
    }

    @Override
    public int setStatus(Seat seat) {
        return seatDao.updateSeatStatus(seat);
    }

    @Override
    public Seat getSeatRowAndCol(Integer seat_id) {
        return seatDao.findSeat(seat_id) ;
    }
}